package com.sky.controller.user;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersStatusAllDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.service.ShoppingCartService;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

import static com.sky.entity.Orders.*;

/**
 * @author yangkexuan
 * @className OrderController
 * @date 2023/6/13 14:05
 * @description
 */
@RestController("userOrderController")
@RequestMapping("/user/order")
@Slf4j
@Api(tags = "C端-订单接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 用户下单
     * @param ordersSubmitDTO
     * @return orderSubmitVO
     */
    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }


    /**
     * 历史订单查询
     * @param --page:1, pageSize:10，  status:订单状态
     * @return 订单状态
     */
    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")
    public Result<PageResult> historyOrders(OrdersPageQueryDTO ordersPageQueryDTO){
        PageResult pageResult = orderService.page(ordersPageQueryDTO);
        return Result.success(pageResult);
    }
    /**
     * 查询订单详情
     * @return OrderVO
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详细")
    public Result<OrderVO> orderDetail(@PathVariable Long id){
        return Result.success(orderService.selectOrderDetailById(id));
    }

    /**
     * 催单
     * @param {id} 订单ID
     * @return null
     */
    @GetMapping("/reminder/{id}")
    @ApiOperation("催单")
    public Result<String> ReminderUser(@PathVariable Long id){
        orderService.ReminderUser(id);
        return Result.success();
    }

    /**
     * 再来一单
     * @param id
     * @return null
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result<String> userRepetition(@PathVariable Long id){
        shoppingCartService.insertAgain(id);
        return Result.success();
    }
    /**
     * 取消订单
     * @param {id}
     * @return null
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result<String> cancelOrder(@PathVariable Long id){
        OrdersStatusAllDTO order = new OrdersStatusAllDTO();
        order.setId(id);
        order.setStatus(CANCELLED);
        order.setCancelTime(LocalDateTime.now());
        orderService.updateCancelOrder(order);
        return Result.success();
    }

    /**
     * 订单支付
     * @return String
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<String> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO){
        orderService.paySuccess(ordersPaymentDTO);
        return Result.success("支付成功");
    }

}
